From 12bc4f1c5b7cef7d9e7e96cea0957f4336fa2a66 Mon Sep 17 00:00:00 2001 From: Brad Jorsch Date: Wed, 9 Oct 2013 11:03:40 -0400 Subject: [PATCH] Handle raw when calculating $rawtoc When the parser is constructing $rawtoc, it needs the sectionIndex number to be able to calculate the byteoffset. This number is only available for wikitext headings ("== foo =="), HTML headings ("

foo

") do not have it and the lack makes byteoffset be wrong for all subsequent headings in the page. To fix this, we just omit output of byteoffset in this situation. Bug: 25203 Change-Id: I39e5faa4ac22d915f06125aac36ced11607b94a3 --- includes/parser/Parser.php | 5 ++- .../includes/parser/ParserMethodsTest.php | 39 +++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/includes/parser/Parser.php b/includes/parser/Parser.php index eac2202ec1..0603a9bc77 100644 --- a/includes/parser/Parser.php +++ b/includes/parser/Parser.php @@ -4447,7 +4447,8 @@ class Parser { # Add the section to the section tree # Find the DOM node for this header - while ( $node && !$isTemplate ) { + $noOffset = ( $isTemplate || $sectionIndex === false ); + while ( $node && !$noOffset ) { if ( $node->getName() === 'h' ) { $bits = $node->splitHeading(); if ( $bits['i'] == $sectionIndex ) { @@ -4465,7 +4466,7 @@ class Parser { 'number' => $numbering, 'index' => ( $isTemplate ? 'T-' : '' ) . $sectionIndex, 'fromtitle' => $titleText, - 'byteoffset' => ( $isTemplate ? null : $byteOffset ), + 'byteoffset' => ( $noOffset ? null : $byteOffset ), 'anchor' => $anchor, ); diff --git a/tests/phpunit/includes/parser/ParserMethodsTest.php b/tests/phpunit/includes/parser/ParserMethodsTest.php index cacbb850df..3cdbf15fc8 100644 --- a/tests/phpunit/includes/parser/ParserMethodsTest.php +++ b/tests/phpunit/includes/parser/ParserMethodsTest.php @@ -44,5 +44,44 @@ class ParserMethodsTest extends MediaWikiLangTestCase { 'text' => '
foo
', ), $ret, 'callParserFunction works for {{#tag:pre|foo|style=margin-left: 1.6em}}' ); } + + public function testGetSections() { + global $wgParser; + + $title = Title::newFromText( str_replace( '::', '__', __METHOD__ ) ); + $out = $wgParser->parse( "==foo==\n

bar

\n==baz==\n", $title, new ParserOptions() ); + $this->assertSame( array( + array( + 'toclevel' => 1, + 'level' => '2', + 'line' => 'foo', + 'number' => '1', + 'index' => '1', + 'fromtitle' => $title->getPrefixedDBkey(), + 'byteoffset' => 0, + 'anchor' => 'foo', + ), + array( + 'toclevel' => 1, + 'level' => '2', + 'line' => 'bar', + 'number' => '2', + 'index' => '', + 'fromtitle' => false, + 'byteoffset' => null, + 'anchor' => 'bar', + ), + array( + 'toclevel' => 1, + 'level' => '2', + 'line' => 'baz', + 'number' => '3', + 'index' => '2', + 'fromtitle' => $title->getPrefixedDBkey(), + 'byteoffset' => 21, + 'anchor' => 'baz', + ), + ), $out->getSections(), 'getSections() with proper value when

is used' ); + } // TODO: Add tests for cleanSig() / cleanSigInSig(), getSection(), replaceSection(), getPreloadText() } -- 2.20.1